/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.garcomeletronico.dao.implementacao;

import br.com.garcomeletronico.bean.Cardapio;
import br.com.garcomeletronico.bean.Prato;
import br.com.garcomeletronico.dao.IDAOPrato;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tulio.luis
 */
public class DAOPrato extends DAOGenerico implements IDAOPrato {

    @Override
    public void inserir(Prato pojo) throws SQLException {
        try {
            String sql = "insert into Prato (cardapio_codigo, nome, preco, descricao)"
                    + "values (?,?,?,?)";
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setInt(1, pojo.getCardapio_codigo());
            stmt.setString(2, pojo.getNome());
            stmt.setFloat(3, pojo.getPreco());
            stmt.setString(4, pojo.getDescricao());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            //throw new SQLException(e.getMessage());
        }
    }

    @Override
    public void alterar(Prato pojo) throws SQLException {
        try {
            String sql = ("update Prato set cardapio_codigo= ?, nome= ?, preco= ?, descricao= ? "
                    + "where codigo= ? ");
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setInt(1, pojo.getCardapio_codigo());
            stmt.setString(2, pojo.getNome());
            stmt.setFloat(3, pojo.getPreco());
            stmt.setString(4, pojo.getDescricao());
            stmt.setInt(5, pojo.getCodigo());
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

    }

    @Override
    public void excluir(Prato pojo) throws SQLException {
        try {
            String sql = "delete from Prato where codigo=?";
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setInt(1, pojo.getCodigo());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }
    }

    @Override
    public List<Prato> listarTodos() throws SQLException {
        String sql = "select * from Prato";
        List<Prato> minhaLista = null;
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = this.getConexao().prepareStatement(sql);
            rs = stmt.executeQuery();

            while (rs.next()) {
                if (minhaLista == null) {
                    minhaLista = new ArrayList<>();
                }

                Prato p = new Prato();
                p.setCodigo(rs.getInt("codigo"));
                p.setNome(rs.getString("nome"));
                p.setPreco(rs.getFloat("preco"));
                p.setDescricao(rs.getString("descricao"));
                p.setCardapio_codigo(rs.getInt("cardapio_codigo"));
                minhaLista.add(p);
            }
            rs.close();
            stmt.close();

        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

        return minhaLista;
    }

    @Override
    public List<Prato> listarPorNome(Prato pojo) throws SQLException {
        String sql = "select * from Prato where nome like '%" + pojo.getNome() + "%'";
        List<Prato> minhaLista = null;
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = this.getConexao().prepareStatement(sql);
            rs = stmt.executeQuery();

            while (rs.next()) {
                if (minhaLista == null) {
                    minhaLista = new ArrayList<>();
                }

                Prato c1 = new Prato();
                c1.setCodigo(rs.getInt("codigo"));
                c1.setNome(rs.getString("nome"));
                c1.setPreco(rs.getFloat("preco"));
                c1.setDescricao(rs.getString("descricao"));
                c1.setCardapio_codigo(rs.getInt("cardapio_codigo"));
                minhaLista.add(c1);
            }
            rs.close();
            stmt.close();

        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

        return minhaLista;
    }

    @Override
    public Prato listarPorId(Prato pojo) throws SQLException {
        String sql = "select * from Prato where codigo = ?";
        Prato retorno = null;
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = this.getConexao().prepareStatement(sql);
            stmt.setInt(1, pojo.getCodigo());
            rs = stmt.executeQuery();

            while (rs.next()) {
                if (retorno == null) {
                    retorno = new Prato();
                }

                retorno.setCodigo(rs.getInt("codigo"));
                retorno.setNome(rs.getString("nome"));
                retorno.setPreco(rs.getFloat("preco"));
                retorno.setDescricao(rs.getString("descricao"));
                retorno.setCardapio_codigo(rs.getInt("cardapio_codigo"));
            }
            rs.close();
            stmt.close();

        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

        return retorno;

    }

    @Override
    public List<Prato> listarPratosPorCardapio(Cardapio cardapio) throws SQLException {
        String sql = "select prato.codigo, Prato.nome, Prato.preco, Prato.descricao from Prato "
                + "where Prato.cardapio_codigo = ? order by Prato.nome";
        List<Prato> retorno = null;
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = this.getConexao().prepareStatement(sql);
            stmt.setInt(1, cardapio.getCodigo());
            rs = stmt.executeQuery();

            while (rs.next()) {
                if (retorno == null) {
                    retorno = new ArrayList<>();
                }

                Prato p = new Prato();
                p.setCodigo(rs.getInt("codigo"));
                p.setNome(rs.getString("nome"));
                p.setPreco(rs.getFloat("preco"));
                p.setDescricao(rs.getString("descricao"));
                p.setCardapio_codigo(cardapio.getCodigo());
                retorno.add(p);
            }
            rs.close();
            stmt.close();

        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }

        return retorno;
    }
}
